package com.mf.util;

import java.util.Random;

import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.hash.Sha1Hash;

/**
 * 字符串工具类
 *
 * @author
 */
public class StringUtil {

    /**
     * 判断是否是空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否不是空
     *
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str) {
        if ((str != null) && !"".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 生成四位编号
     *
     * @param code
     * @return
     */
    public static String formatCode(String code) {
        int length = code.length();
        Integer num = Integer.parseInt(code.substring(length - 4, length)) + 1;
        String codeNum = num.toString();
        int codeLength = codeNum.length();
        for (int i = 4; i > codeLength; i--) {
            codeNum = "0" + codeNum;
        }
        return codeNum;
    }

    /**
     * 随机生成n位字符数字混合码
     *
     * @param num //生成数
     */
    public static String randomKey(int num) {
        Random random = new Random();
        String code = "qwertyuioplkjhgfdsazxcvbnmAQWERTYUIOPLKJHGFDSZXCVBNM0123456789";
        StringBuffer buffer = new StringBuffer();
        int range = code.length();

        for (int i = 0; i <= num; i++) {
            buffer.append(code.charAt(random.nextInt(range)));
        }

        return buffer.toString();
    }

    /**
     * 获取HEX ENCODING 加密文字
     *
     * @param args
     * @return;
     */
    public static byte[] saltBytes() {
        String code = "Yinetsrl123..!!@@";
        return code.getBytes();
    }

    public static void main(String[] args) {
        String uString = StringUtil.randomKey(8);
        System.out.println(uString);
        System.out.println(formatCode("JH201712010002"));
    }
}
